<template>
  <!-- 搜索历史组件模块 -->
  <div class="search-history">
    <van-cell title="搜索历史">
      <!-- isDeleteShow 默认为false, 默认显示 v-else 的删除按钮 -->
      <div v-if="isDeleteShow">
      <span @click="$emit('clear-search-histories')">全部删除</span>
      &nbsp;&nbsp;
      <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true"></van-icon>
    </van-cell>
    <van-cell
    v-for="(item, index) in searchHistories"
    :key="index"
    :title="item"
    @click="onSearchItemClick(item, index)"
    >
      <!-- 关闭按钮 close -->
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    onSearchItemClick (item, index) {
      // 如果是删除状态的话, 点击删除历史记录
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
      } else {
        // 如果不是 则跳转到搜索
        this.$emit('searchdelete', item)
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
